package com.tc.juc.pack06;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicReference;

/**
 * @Auther: tianchao
 * @Date: 2021/10/5 21:28
 * @Description:
 */
@Slf4j
public class Test36 {
    static AtomicReference<String> atomicReference = new AtomicReference<>("A");

    public static void main(String[] args) {
        log.info("main start....");
        String prev = atomicReference.get();
        other();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("尝试 a -> c: {}", atomicReference.compareAndSet(prev, "C"));
        log.info("修改后的值: {}", atomicReference.get());
    }

    private static void other() {
        new Thread(()->{log.info("尝试 a -> b: {}",atomicReference.compareAndSet(atomicReference.get(), "B"));}).start();
        new Thread(()->{log.info("尝试 b -> a: {}",atomicReference.compareAndSet(atomicReference.get(), "A"));}).start();
    }
}
